/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans;

import com.easoft.code.pos.App;
import com.easoft.code.pos.data.exception.CoreDataLayerException;
import com.easoft.code.pos.data.pojos.Persona;
import com.easoft.code.pos.ui.controllers.POSController;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Eliu
 */
public class FormPOSDatosCliente extends javax.swing.JDialog {

    private POSController controller;
    private Persona cliente;

    public Persona getCliente() {
        return cliente;
    }

    public void setCliente(Persona cliente) {
        this.cliente = cliente;
    }

    /**
     * Creates new form FormPOSDatosCliente
     *
     * @param controller
     * @param c
     */
    public FormPOSDatosCliente(POSController controller, Persona c) {
        super(App.mainForm, "Datos Cliente", true);
        this.controller = controller;
        Frame parent = App.mainForm;
        if (parent != null) {
            Dimension parentSize = parent.getSize();
            Point p = parent.getLocation();
            setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
        }
        initComponents();
        if (c != null) {
            preloadCliente(c);
        }
        setSize(new Dimension(410, 220));
        setVisible(true);
    }

    private void preloadCliente(Persona c) {
        cliente = c;
        txtNit.setText(cliente.getCodigo());
        txtNombre.setText(cliente.getNombre());
        txtDireccion.setText(cliente.getDireccion());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNit = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Datos de Cliente");
        setResizable(false);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Nit");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, -1));

        txtNit.setNextFocusableComponent(txtNombre);
        txtNit.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNitFocusLost(evt);
            }
        });
        txtNit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNitKeyPressed(evt);
            }
        });
        jPanel1.add(txtNit, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 20, 240, -1));

        txtNombre.setNextFocusableComponent(txtDireccion);
        jPanel1.add(txtNombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 240, -1));

        jLabel2.setText("Nombre");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, -1, -1));
        jPanel1.add(txtDireccion, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 80, 240, -1));

        jLabel3.setText("Direccion:");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 80, -1, -1));

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tick.png"))); // NOI18N
        jButton1.setText("Continuar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton1);

        jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            if (txtNit.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(App.mainForm,
                        "Debe ingresar el NIT!",
                        "Validacion",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (txtNombre.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(App.mainForm,
                        "Debe ingresar el nombre!",
                        "Validacion",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            cliente = controller.validateCliente(txtNit.getText(),
                    txtNombre.getText(), txtDireccion.getText(), true);
            if (cliente != null) {
                setVisible(false);
            } else {
                txtNombre.setText("");
                txtDireccion.setText("");
            }
        } catch (CoreDataLayerException ex) {
            ex.printStackTrace(System.err);
            JOptionPane.showMessageDialog(rootPane, ex);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtNitKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNitKeyPressed
        if (evt.getKeyCode() == 10) {
            try {
                cliente = controller.validateCliente(txtNit.getText(),
                        txtNombre.getText(), txtDireccion.getText(), false);
                if (cliente != null) {
                    preloadCliente(cliente);
                }
            } catch (CoreDataLayerException ex) {
                Logger.getLogger(FormPOSDatosCliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_txtNitKeyPressed

    private void txtNitFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNitFocusLost
        try {
            cliente = controller.validateCliente(txtNit.getText(),
                    txtNombre.getText(), txtDireccion.getText(), false);
            if (cliente != null) {
                preloadCliente(cliente);
            }
        } catch (CoreDataLayerException ex) {
            Logger.getLogger(FormPOSDatosCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_txtNitFocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtNit;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
